# Generated by Django 2.2.9 on 2020-03-04 11:46

from django.contrib.auth import get_user_model
from django.db import migrations
from rolepermissions.checkers import has_role
from rolepermissions.permissions import revoke_permission
from rolepermissions.roles import assign_role


def reset_power_roles(apps, schema_editor):
    User = get_user_model()
    for user in User.objects.all():
        if has_role(user, "power_user"):
            # Re-assigning the role will sync the default associated permissions
            assign_role(user, "power_user")


def revoke_settings_crud_from_power_users(apps, schema_editor):
    User = get_user_model()
    for user in User.objects.all():
        if has_role(user, "power_user"):
            revoke_permission(user, "settings_crud")
            user.save()


class Migration(migrations.Migration):
    dependencies = [
        ("user", "0052_allow_power_user_to_set_cresus_number"),
    ]

    operations = [
        migrations.RunPython(reset_power_roles, revoke_settings_crud_from_power_users),
    ]
